//
//  DJDetailCommentTableViewCell.m
//  CoolFM
//
//  Created by 曹曹 on 16/9/10.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "DJDetailCommentTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
#import "Comment.h"
#import "Reply.h"
#import "DJDetailComment.h"
#import "CFMacro.h"

@interface DJDetailCommentTableViewCell ()

// 头像
@property (nonatomic, strong) UIImageView *avatarView;
// 昵称
@property (nonatomic, strong) UILabel *nickNameLabel;
// 评论时间
@property (nonatomic, strong) UILabel *commentTime;
// 用户评论
@property (nonatomic, strong) UILabel *commentLabel;
// 评论他人分割线
@property (nonatomic, strong) UIImageView *seperatorLineView;
// 存放其他评论的容器view
@property (nonatomic, strong) UIView *containerView;
// 昵称 顶部距离cell的距离
@property (nonatomic, assign) CGFloat nickTopMargin;
// 评论之间的距离
@property (nonatomic, assign) CGFloat commentMargin;
// 评论字体大小
@property (nonatomic, assign) UIFont *commentFont;
//
@property (nonatomic, strong) UILabel *replyLabel;

@end

@implementation DJDetailCommentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubViews];
    }
    return self;
}
#pragma mark - model setter
- (void)setComment:(Comment *)comment {
    _comment = comment;
    [_avatarView sd_setImageWithURL:[NSURL URLWithString:comment.comment_user_image_url]
                   placeholderImage:[UIImage imageNamed:@"img_dj_head"]];
    _nickNameLabel.text = comment.comment_from_user_name;
    _commentTime.text = comment.comment_create_at;
    _commentLabel.text = comment.comment_content;
}
#pragma mark - 高度字典, 在这里判断是否有回复, 有则创建 label
- (void)setSubViewsHeights:(NSDictionary *)subViewsHeights {
    _subViewsHeights = subViewsHeights;
    NSString *height = _subViewsHeights[@"commentH"];
    CGFloat h = ceil([height floatValue]);
    // 评论的高度
    _commentLabel.gx_height = h;
    // 有他人回复时
    if (_comment.reply.count > 0) {
        // 分割线的Y
        CGFloat seperatorLineViewY = _commentLabel.gx_y + _commentLabel.gx_height + self.commentMargin;
        _seperatorLineView.gx_y = seperatorLineViewY;
        // 容器的Y
        CGFloat containerViewY = _seperatorLineView.gx_y + _seperatorLineView.gx_height + self.commentMargin;
        _containerView.gx_y = containerViewY;
        // 遍历所有的回复
        NSArray *array = _subViewsHeights[@"replys"];
        CGFloat replyH = 0;
        // 别放到for循环里边, 否则不会有累加高度, 使纵坐标的值不正确
        CGFloat labelY = 0;
        for (int i = 0; i < _comment.reply.count; i++) {
            // 创建回复label
            CGFloat labelH = ceil([array[i] floatValue]);
#warning 计算回复label的纵坐标还有问题
            if (_comment.reply.count > 1) {
                if (i > 0) {
                    labelY += ceil([array[i - 1] floatValue]);
                }
            }
            UILabel *replyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, labelY, _nickNameLabel.gx_width, labelH)];
            NSString *replyStr = [NSString stringWithFormat:@"%@ 回复 %@ :%@", _comment.reply[i].reply_from_user_name, _comment.reply[i].reply_comment_to_user_name, _comment.reply[i].reply_content];
            /**
             * 设置属性字体
             */
            NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:replyStr];
            NSRange huifu = [replyStr rangeOfString:@"回复"];
            [attribute addAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} range:huifu];
            [attribute addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:10]} range:huifu];
            /**
             * 主播颜色字体
             */
            if (_comment.reply[i].reply_from_user_dj_status == 1) {
                NSRange dj = [replyStr rangeOfString:_comment.reply[i].reply_from_user_name];
                [attribute addAttributes:@{NSForegroundColorAttributeName: kDefaultColor} range:dj];
                NSLog(@"%@ is DJ", _comment.reply[i].reply_from_user_name);
            }
            [replyLabel setAttributedText:attribute];
            replyLabel.font = kFont11;
            replyLabel.textAlignment = NSTextAlignmentLeft;
            replyLabel.numberOfLines = 0;
            [_containerView addSubview:replyLabel];
            replyH += labelH;
        }
        _containerView.gx_height = replyH;
        _seperatorLineView.gx_height = replyH > 0 ? 1 : 0;
    }
}
#pragma mark - 创建控件
- (void)setUpSubViews {
    [self.contentView addSubview:self.avatarView];
    [self.contentView addSubview:self.nickNameLabel];
    [self.contentView addSubview:self.commentTime];
    [self.contentView addSubview:self.commentLabel];
    [self.contentView addSubview:self.seperatorLineView];
    [self.contentView addSubview:self.containerView];
}
- (CGFloat)nickTopMargin {
    return 20;
}
- (UIFont *)commentFont {
    UIFont *font = kFont11;
    return font;
}
- (CGFloat)commentMargin {
    return 5;
}
- (UIImageView *)avatarView {
    UIImageView *avatar = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
    avatar.layer.cornerRadius = 30;
    avatar.layer.masksToBounds = YES;
    avatar.backgroundColor = [UIColor redColor];
    _avatarView = avatar;
    return _avatarView;
}
- (UILabel *)nickNameLabel {
    CGFloat x = _avatarView.gx_x + _avatarView.gx_width + 10;
    CGFloat y = self.nickTopMargin;
    CGFloat w = CGRectGetWidth([UIScreen mainScreen].bounds) - _avatarView.gx_width - 30;
    CGFloat h = 20;
    UILabel *nick = [UILabel new];
    [nick setFrame:CGRectMake(x, y, w, h)];
    nick.font = self.commentFont;
    _nickNameLabel = nick;
    return _nickNameLabel;
}
- (UILabel *)commentTime {
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds) - 110, 5, 100, 10)];
    timeLabel.text = @"26天前";
    timeLabel.textColor = [UIColor lightGrayColor];
    timeLabel.textAlignment = NSTextAlignmentRight;
    timeLabel.font = [UIFont systemFontOfSize:9];
    _commentTime = timeLabel;
    return _commentTime;
}
- (UILabel *)commentLabel {
    CGFloat x = _nickNameLabel.gx_x;
    CGFloat y = self.nickTopMargin + _nickNameLabel.gx_height + self.commentMargin;
    CGFloat w = _nickNameLabel.gx_width;
    CGFloat h = 0;
    UILabel *comment = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)];
    comment.font = self.commentFont;
    comment.numberOfLines = 0;
    _commentLabel = comment;
    return _commentLabel;
}
- (UIImageView *)seperatorLineView {
    CGFloat x = _nickNameLabel.gx_x;
    // 相对值, 跟commentLabel的高度有关, 高度字典传入时在给值
    CGFloat y = 0;
    CGFloat w = _nickNameLabel.gx_width;
    // 同样也是相对值
    CGFloat h = 0;
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    _seperatorLineView = lineView;
    return _seperatorLineView;
}
- (UIView *)containerView {
    CGFloat x = _nickNameLabel.gx_x;
    CGFloat y = 0;
    CGFloat w = _nickNameLabel.gx_width;
    CGFloat h = 0;
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
    _containerView = container;
    return _containerView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
